home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / StandardFileIcons / SFIcons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-23  |  7.0 KB  |  267 lines  |  [TEXT/MPS ]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    StandardFileIcons: A sample control panel changing behavior of StandardFile.
  5. **
  6. **    by Brian Bechtel, Apple Developer Technical Support
  7. **
  8. **    File:        SFIcons.c
  9. **
  10. **    Copyright © 1994 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DTS Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. **
  21. ** Change history (most recent first)
  22. **
  23. **    <1>    940811    BL°B    Check for System 7.  Display failure icon and do
  24. **                        nothing else if earlier than System 7.  Macintosh
  25. **                        Easy Open is the first product which actually
  26. **                        provided the desktop icon feature; it can work all
  27. **                        the way back to System 7, potentially.
  28. **    <0>    940802    BL°B    First implementation
  29. **/
  30.  
  31.  
  32. #include    <types.h>
  33. #include    <memory.h>
  34. #include    <events.h>
  35. #include    <dialogs.h>
  36. #include    <controls.h>
  37. #include     <files.h>
  38. #include    <Devices.h>
  39. #include     <GestaltEqu.h>
  40. #include    <Folders.h>
  41. #include    <Errors.h>
  42. #include    <Script.h>
  43. #include    <ToolUtils.h>
  44. #include    <Resources.h>
  45.  
  46. #include "GestaltValue.h"
  47.  
  48. enum {
  49.     gestaltStandardFileUseGenericIcons = 3,
  50.     kSFIconsPrefFileName = 128
  51. };
  52.  
  53. pascal Handle main(short, short, short, short, EventRecord *, Handle, DialogPtr);
  54. Handle InitStorage(void);
  55. void DisposeStorage(Handle);
  56. void SetButton(short, DialogPtr, short);
  57. void DoHit(short, short, DialogPtr, Handle);
  58. void Generic(Handle);
  59. void CustomIcons(Handle);
  60. void ReportStatus(short, DialogPtr);
  61. Boolean UseGenericIcons(void);
  62. void CreatePreferenceFile(Handle);
  63. void DeletePreferenceFile(Handle);
  64. Boolean OKSystem(void);
  65.  
  66. /* 
  67.  * This dispatcher routine must be the first code, and it needs to follow the Pascal
  68.  * parameter passine mechanisms.
  69.  */
  70.  
  71. pascal Handle main(short message, 
  72.                    short item, 
  73.                    short numItems, 
  74.                    short CPanelID, 
  75.                    EventRecord *theEvent, 
  76.                    Handle cdevStorage, 
  77.                    DialogPtr CPDialog)
  78. {
  79. #pragma unused(CPanelID, theEvent)
  80.  
  81. if (message == macDev) {     /* We should never get this message, but if we do… */
  82.         if (!OKSystem())
  83.             return (Handle)0;
  84.         else
  85.             return (Handle)1;        /* …always show up in the Control Panel unless pre-System 7. */
  86.     } else {
  87.         if (cdevStorage) {            /* cdevStorage = 0 if we have an error -- ignore events */
  88.             switch (message) {        /* What are we supposed to do this call? */
  89.                 case initDev:                /* Take care of initialization */
  90.                     cdevStorage = InitStorage();
  91.                     if (cdevStorage) {        /* Allocate our storage */
  92.                         ReportStatus(numItems, CPDialog);    /* Tell what the disk is now. */
  93.                     }
  94.                     break;
  95.                 case closeDev:            /* Free the storage that we used */
  96.                     DisposeStorage(cdevStorage);
  97.                     break;
  98.                 case hitDev:                /* The user clicked in the CP */
  99.                     HLock(cdevStorage);
  100.                     DoHit(item, numItems, CPDialog,cdevStorage);
  101.                     HUnlock(cdevStorage);
  102.                     break;
  103.             }
  104.             return cdevStorage;        /* Return the cdevStorage. CP will put up alert if 0 */
  105.         }
  106.     }
  107. }
  108.  
  109.  
  110. Handle InitStorage(void)
  111. {
  112.     return (Handle)GetString(kSFIconsPrefFileName);
  113. }
  114.  
  115.  
  116. void DisposeStorage(Handle h)
  117. {
  118.     ReleaseResource(h);
  119. }
  120.  
  121.  
  122. void SetButton(short item, DialogPtr dialog, short value)
  123. {
  124.     short            itemType;
  125.     Handle        itemHandle;
  126.     Rect            itemBox;
  127.     
  128.     GetDItem(dialog, item, &itemType, &itemHandle, &itemBox);    /* Get the ControlHandle */
  129.     SetCtlValue((ControlHandle)itemHandle, value); /* And set the button value */
  130. }
  131.  
  132.  
  133. void DoHit(short item, short numItems, DialogPtr CPDialog, Handle preferenceFileName)
  134. {
  135.     short        i;
  136.     
  137.     for (i = 1; i <= 2; i++) {                            /* Do each of our 2 radio buttons */
  138.         if (i == (item - numItems)) {                    /* Is this the button clicked in? */
  139.             SetButton(i+numItems, CPDialog, 1);            /* Yes. Turn it on. */
  140.             if (i == 2)
  141.                 Generic(preferenceFileName);            /* use generic icons. */
  142.             else
  143.                 CustomIcons(preferenceFileName);
  144.         } else {
  145.             SetButton(i+numItems, CPDialog, 0);    /* Nope. Turn others off. */
  146.         }
  147.     }
  148. }
  149.  
  150.  
  151. /*
  152.  * Generic
  153.  * 
  154.  * Set the Gestalt bit in gestaltStandardFileAttr which says to use Generic Icons
  155.  * Create a preference file.  The INIT in this control panel will look for the
  156.  * preference file in order to decide what behavior to set at startup time.
  157.  */
  158. void Generic(Handle preferenceFileName)
  159. {
  160.     long    gestaltValue;
  161.     OSErr    err;
  162.     
  163.     err = Gestalt(gestaltStandardFileAttr, &gestaltValue);
  164.     if (!err)
  165.     {
  166.         gestaltValue |= (1 << gestaltStandardFileUseGenericIcons);
  167.         ReplaceGestaltValue(gestaltStandardFileAttr, gestaltValue);
  168.         CreatePreferenceFile(preferenceFileName);
  169.     }
  170. }
  171.  
  172.  
  173.  
  174. /*
  175.  * CustomIcons
  176.  * 
  177.  * Clear the Gestalt bit in gestaltStandardFileAttr which says to use Generic Icons
  178.  * Delete a preference file if it exists.  The INIT in this control panel will look 
  179.  * for the preference file in order to decide what behavior to set at startup time.
  180.  */
  181. void CustomIcons(Handle preferenceFileName)
  182. {
  183.     long    gestaltValue;
  184.     OSErr    err;
  185.     
  186.     err = Gestalt(gestaltStandardFileAttr, &gestaltValue);
  187.     if (!err)
  188.     {
  189.         gestaltValue &= ~(1 << gestaltStandardFileUseGenericIcons);
  190.         ReplaceGestaltValue(gestaltStandardFileAttr, gestaltValue);
  191.         DeletePreferenceFile(preferenceFileName);
  192.     }
  193. }
  194.  
  195.  
  196. void ReportStatus(short numItems, DialogPtr CPDialog)
  197. {    
  198.     if (UseGenericIcons()) {
  199.             SetButton(2+numItems, CPDialog, 1);    /* Set the 'black and white' button */
  200.             SetButton(1+numItems, CPDialog, 0);    /* Turn off 'color' button */
  201.     } else {
  202.             SetButton(1+numItems, CPDialog, 1);    /* Set the 'color' button */
  203.             SetButton(2+numItems, CPDialog, 0);    /* Turn off 'black and white' button */
  204.     }
  205. }
  206.  
  207.  
  208. /* Decide to use generic icons or not. */
  209. Boolean UseGenericIcons(void)
  210. {
  211.     long    response;
  212.     OSErr    err;
  213.     
  214.     err = Gestalt(gestaltStandardFileAttr, &response);
  215.     if (!err)
  216.     {
  217.         return(response & (1 << gestaltStandardFileUseGenericIcons)); 
  218.     }
  219.     else
  220.         return false;
  221. }
  222.  
  223.  
  224. /* Create our preference file in the Preference folder */
  225. void CreatePreferenceFile(Handle preferenceFileName)
  226. {
  227.     OSErr    err;
  228.     FSSpec    spec;
  229.     short    foundVRefNum;
  230.     long    foundDirID;
  231.     
  232.     err = FindFolder(kOnSystemDisk,kPreferencesFolderType,false,&foundVRefNum,&foundDirID);
  233.     if (!err)
  234.         err = FSMakeFSSpec(foundVRefNum,foundDirID,(StringPtr)*preferenceFileName,&spec);
  235.     if ((err == noErr) || (err == fnfErr))
  236.         err = FSpCreate(&spec,'SFI#','PREF',smCurrentScript);
  237. }
  238.  
  239.  
  240. /* Delete our preference file in the preference folder */
  241. void DeletePreferenceFile(Handle preferenceFileName)
  242. {
  243.     OSErr    err;
  244.     FSSpec    spec;
  245.     short    foundVRefNum;
  246.     long    foundDirID;
  247.     
  248.     err = FindFolder(kOnSystemDisk,kPreferencesFolderType,false,&foundVRefNum,&foundDirID);
  249.     if (!err)
  250.         err = FSMakeFSSpec(foundVRefNum,foundDirID,(StringPtr)*preferenceFileName,&spec);
  251.     if (!err)
  252.         err = FSpDelete(&spec);
  253. }
  254.  
  255.  
  256. /* Check System Version and return false if primary digit is < 7 (e.g. System 6)*/
  257. Boolean OKSystem(void)
  258. {
  259.     long    response;
  260.     OSErr    err;
  261.     
  262.     err = Gestalt(gestaltSystemVersion, &response);
  263.     
  264.     return ( (err == noErr) && ((response >> 8) >= 0x07) );
  265. }
  266.     
  267.